package com.example.mqspring.rabiitmqbasicleverone.rabbitmqRPC;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;
import java.util.Map;

/**
 * 这是RPC请求方对服务方回馈的消息进行消费
 */
public class RPCClientConsume extends DefaultConsumer {

    private Map<String,String> actions;
    /**
     */
    public RPCClientConsume(Channel channel,Map<String,String> actions) {
        super(channel);
        this.actions = actions;
    }

    @Override
    public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {



        String messageIdentifier = properties.getCorrelationId();

        String action = actions.get(messageIdentifier);

        actions.remove(messageIdentifier);

        String response = new String(body);
//        处理动作和消息
        onReply(action,response);
    }

    public void onReply(String action,String message){
        System.out.println("回复是:"+message);
        System.out.println("动作是:"+action);
    }
}
